﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.IO;

namespace TED
{
    public class TalkManager
    {
        private DataSet talkData = null;
    
        public DataTable Talks
        {
            get 
            {
                if (talkData != null)
                    return talkData.Tables[TableNames.talks];
                return null;
            }
        }
        public DataTable Themes
        {
            get 
            {
                if (talkData != null)
                    return talkData.Tables[TableNames.themes];
                return null;
            }
        }

        /// <summary>
        /// Attempts to set the download path as the path being sent in as a parameter. 
        /// If an error occurs, the path will be taken to be My Videos.
        /// </summary>
        /// <param name="theme"></param>
        /// <param name="tryDownloadPath"></param>
        /// <returns></returns>
        public string GetDownloadPath(string theme,string tryDownloadPath)
        {
            string path = Path.Combine(tryDownloadPath, @"TED\" + theme);

            bool tryMakeFolder = true;
            while (tryMakeFolder)
            {
                try
                {
                    if (!Directory.Exists(path))
                    {
                        Directory.CreateDirectory(path);
                    }
                    tryMakeFolder = false;
                }
                catch (Exception exc)
                {
                    if (exc is IOException || exc is NotSupportedException)
                    {
                        string compare = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyVideos), @"TED\" + theme);
                        if (compare != path)
                            path = compare;
                        else
                            tryMakeFolder = false;
                    }
                    else
                        throw exc;
                }
            }
            return path;
        }
        public bool LoadData(string convertedDataPath)
        {
            string schemaPath = Path.Combine(convertedDataPath, TEDConstants.dataSetSchemaFileName);
            string dataPath = Path.Combine(convertedDataPath, TEDConstants.dataSetDataFileName);

            if (Directory.Exists(convertedDataPath) && File.Exists(schemaPath) && File.Exists(dataPath))
            {
                talkData = new DataSet(Path.Combine(convertedDataPath, TEDConstants.dataSetDataFileName));
                talkData.ReadXmlSchema(schemaPath);
                talkData.ReadXml(dataPath);
                return true;
            }
            return false;
        }
        public bool SaveData(string outputPath)
        {
            return DataSaver.SaveDataSet(this.talkData, outputPath);
        }
    }
}
